import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class PainelFerramenta extends JPanel implements MouseListener,
		MouseMotionListener {

	private JButton botao;
	private JLabel tempo;
	private JButton play;

	private Object referencia;
	float time = -1;

	public PainelFerramenta() {

		setSize(1000, 100);
		tempo = new JLabel("00");
		setBorder(LineBorder.createBlackLineBorder());
		add(tempo);

		botao = new JButton("o");
		botao.setBounds(50, 30, 50, 50);
		tempo.setBounds(5, 30, 50, 50);
		botao.addMouseListener(this);
		botao.addMouseMotionListener(this);

		play = new JButton(".");
		play.setBounds(0, 20, 20, 20);
		play.addMouseListener(this);

		this.addMouseMotionListener(this);
		add(botao);
		add(play);
		setLayout(null);
		// this.getGraphics().fillRect(50,50,950,10);
	}

	public void paintComponent(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0, 0, 1000, 1000);
		g.setColor(Color.black);
		g.fillRect(50, 50, 950, 10);
		for (int i = 0; i < 10; i++) {
			g.drawLine((50 + 90 * (i + 1)), 35, (50 + 90 * (i + 1)), 60);
		}

	}

	public void getButton() {
		return;
	}

	public void setTempo(String tempos) {
		tempo.setText(tempos);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

		if (arg0.getSource() == botao) {
			if (referencia == null)
				referencia = botao;

			System.out.println("clicou");
		} else {
			if (referencia != null) {
				botao.setLocation(arg0.getX(), botao.getY());
				if (arg0.getX() >= 50 && arg0.getX() <= 900) {
					time = (float) (arg0.getX() / 90.0);
					String num = time + "";
					System.out.println(num.length());
					if (num.length() > 3)
						tempo.setText(num.substring(0, 3));
					else
						tempo.setText(num);

				}
				referencia = null;
				updateUI();

				Visualizacao.getInstance().midiasAtivasMomentoCorrente(
						(int) Float.parseFloat(tempo.getText()));
			repaint();
			}

		}

		if (play == arg0.getSource()) {
			Simulacao s = new Simulacao(tempo, botao);
			s.start();
		}

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("pressionou");

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// System.out.println("moveu");
		// if( arg0.getSource()==botao){
		// referencia=botao;
		//		
		// }
		// System.out.println("moveu");
		// else
		// referencia=null;
		// }
	}

}
